var Blocks = {    
    
    resetForm : function(selector){
        $(selector).next().attr("disabled", "disabled");
        $(selector).next().html("<option value=\"\"></option>");
    }, 
    
    load : function(selector){
        $.ajax({
            type: "POST",
            url: Cyfy.getSourcePath('ajax.php'),
            data: "module=Cyfy&ajax=loadBlocksList&class=" + selector.value,
            async: true,
            dataType: "json",
            success: function(data){
                var ids = data.ids.split("\n");
                var names = data.names.split("\n");
                // prepares the select
                $(selector).next().removeAttr("disabled");
                var options = "<option value=\"\"></option>";
                for(var i=0; i<ids.length; i++){
			if(ids[i]){
				options += "    <option value=\""+ids[i]+"\">"+names[i]+"</option>\n";
			}
                }
             $(selector).next().html(options);
        }});
    },
	
	reload : function (module, block, send, callback){
		var className = "." + module + "__" + block;
		$(className).wrap("<div class=\"blockReloadHelper\" />");
		$.ajax({
            type: "POST",
            url: Cyfy.getSourcePath('ajax.php'),
            data: "module=Cyfy&ajax=getBlock&class=" + module +"&block=" + block + "&url=" + Cyfy.query + "&" + send,
            async: true,
            dataType: "json",
            success: function(data){
                $(".blockReloadHelper").html(data.text);
				$(className).unwrap();
				if(callback) eval(callback);
        }});
	}
}

$(document).ready(function(){
    $(".blocks-blocks").attr("disabled", "disabled")
    $(".blocks-modules").change(function(){
        if(this.value){
            Blocks.load(this);
        }else{
            Blocks.resetForm(this);
        }        
    });
});
